package controllers

import (
	"fmt"
	"go-web/models"
	"strconv"

	"github.com/gin-gonic/gin"
)

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

type UserController struct{}

func (u UserController) Login(c *gin.Context) {
	var loginRequest LoginRequest

	// 使用 ShouldBindJSON 绑定请求体数据到结构体
	if err := c.ShouldBindJSON(&loginRequest); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	// 打印传递过来的值
	fmt.Println("Username:", loginRequest.Username)
	fmt.Println("Password:", loginRequest.Password)
	ReturnSuccess(c, 0, "success", "请求成功", 1)
}

func (u UserController) UserInfo(c *gin.Context) {
	// 获取传递过来的参数
	id := c.Param("id")
	ReturnSuccess(c, 0, "success", id, 1)
}

func (u UserController) UserList(c *gin.Context) {
	// 获取传递过来的参数
	idStr := c.Query("id")
	name := c.DefaultQuery("name", "杨胖胖")
	// 转成int
	id, _ := strconv.Atoi(idStr)
	user, _ := models.GetUser(id)
	ReturnSuccess(c, 0, name, user, 1)
}

func (u UserController) UserError(c *gin.Context) {
	num1 := 1
	num2 := 0
	num3 := num1 / num2

	ReturnError(c, 500, num3)
}
